﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinForm
{
    public partial class ChiTietDonHangForm : Form
    {
        public ChiTietDonHangForm()
        {
            InitializeComponent();
        }

        private Library.BIZ.HoaDonXuatBIZ hoadon;
        private List<Library.BIZ.ChiTietHoaDonXuatBIZ> listchitiet;
        private List<ChiTietHoaDon1> listchitiet1 = new List<ChiTietHoaDon1>();

        public ChiTietDonHangForm(Library.BIZ.HoaDonXuatBIZ h)
        {
            InitializeComponent();
            this.hoadon = h;
            if (this.hoadon.TrangThai == true)
            {
                button1.Enabled = false;
            }
        }

        private void ChiTietDonHangForm_Load(object sender, EventArgs e)
        {
            label1.Text += " " + hoadon.Ngay;
            label2.Text += " " + hoadon.GiaTri;
            label3.Text += " " + hoadon.TenKH;
            label4.Text += " " + hoadon.DiaChiKH;
            label5.Text += " " + hoadon.SoDienThoaiKH;

            listchitiet = Library.DAL.ChiTietHoaDonXuatDAL.FindByHoaDon(hoadon);

            foreach (Library.BIZ.ChiTietHoaDonXuatBIZ i in listchitiet)
            {
                ChiTietHoaDon1 c = new ChiTietHoaDon1();
                c.IDSP = i.ID_SanPham;
                c.TenSP = new Library.BIZ.SanPhamBIZ(c.IDSP).TenSP;
                c.MaSP = new Library.BIZ.SanPhamBIZ(c.IDSP).MaSP;
                c.DonGia = i.DonGia.ToString();
                c.SoLuong = i.SoLuong.ToString();

                listchitiet1.Add(c);
            }

            dataGridView1.DataSource = listchitiet1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.SelectedIndex == -1)
                {
                    MessageBox.Show("Chưa chọn cách xử lý");
                    return;
                }
                else if (comboBox1.SelectedIndex == 0)
                {
                    foreach (Library.BIZ.ChiTietHoaDonXuatBIZ i in listchitiet)
                    {
                        Library.BIZ.ChiTietKhoBIZ kho = new Library.BIZ.ChiTietKhoBIZ();
                        kho.Get(new Library.BIZ.SanPhamBIZ(i.ID_SanPham), DateTime.Now);
                        if (kho.SoLuong < i.SoLuong)
                        {
                            MessageBox.Show("Sản phẩm có ID " + i.ID_SanPham + " không đủ số lượng");
                            return;
                        }
                    }

                    hoadon.HoanTatHoaDon();
                    foreach (Library.BIZ.ChiTietHoaDonXuatBIZ i in listchitiet)
                    {
                        Library.BIZ.ChiTietKhoBIZ kho = new Library.BIZ.ChiTietKhoBIZ();
                        kho.Get(new Library.BIZ.SanPhamBIZ(i.ID_SanPham), DateTime.Now);
                        int sl = Int32.Parse(kho.SoLuong.ToString());

                        kho = new Library.BIZ.ChiTietKhoBIZ();

                        kho.ID_SanPham = i.ID_SanPham;
                        kho.Ngay = DateTime.Now;
                        kho.SoLuong = sl - i.SoLuong;
                        kho.Add();
                    }
                    MessageBox.Show("Đã xử lý xong");
                    this.Close();
                }
                else if (comboBox1.SelectedIndex == 1)
                {
                    this.Close();
                }
                else if (comboBox1.SelectedIndex == 2)
                {
                    if (MessageBox.Show("Bạn có muốn xóa ?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        foreach (Library.BIZ.ChiTietHoaDonXuatBIZ i in listchitiet)
                        {
                            i.Delete();
                        }

                        hoadon.Delete();
                    }
                    this.Close();
                }
            }
            catch
            {
            }
        }
    }

    public class ChiTietHoaDon1
    {
        public int IDSP { get; set; }
        public string MaSP { get; set; }
        public string TenSP { get; set; }
        public string DonGia { get; set; }
        public string SoLuong { get; set; }
    }
}
